<!-- Sofija Puric 6/08 -->
<?php


 include_once dirname(__FILE__).'/db.php';
 include_once  dirname(__FILE__).'/../inc_mailer/class.phpmailer.php';
 include_once dirname(__FILE__).'/../inc_mailer/class.smtp.php';


function check_beds($beds) {
    $beds = trim($beds);
    if (is_numeric($beds) && ($beds > 0))
        return true;
    else return false;
}

function check_date($date) {        
        $date = trim($date);
        if (preg_match("/^([1-2][0-9]|[3][0-1]|[0][1-9]|[1-9])\/([0][1-9]|[1][0-2]|[1-9])\/([0-9]{4})$/", $date)) {
        $date_array = explode('/', $date);
        $day = $date_array[0];
        $month = $date_array[1];
        $year = $date_array[2]; 
        if ($month == 2){
			if(($year%4 == 0) && (!($year%100==0) || ($year%400==0))) {
			        if ($day > 29)
                                    return false;
			}
			else { 
                            if ($day > 28)
                                return false;
			}
        }
        else {
          if (($day > 30) && (($month == 4) || ($month == 6) || ($month == 9) || ($month == 11)))
	  	return false;
	  }
	return true;
    }
    else {
        return false;
}
     
    
}

function check_dates($date_f, $date_t) {     
    $date_f = trim($date_f);
    $date_t = trim($date_t);
    $date_from_array = explode('/', $date_f);
    $date_to_array = explode('/', $date_t);
    $day_from = $date_from_array[0]; $month_from =  $date_from_array[1]; $year_from =  $date_from_array[2];
    $day_to = $date_to_array[0]; $month_to =  $date_to_array[1]; $year_to =  $date_to_array[2];
    if (strlen($day_from)<2) $day_from='0'.$day_from; if (strlen($month_from)<2) $month_from='0'.$month_from;
    if (strlen($day_to)<2) $day_to='0'.$day_to; if (strlen($month_to)<2) $month_to='0'.$month_to;
    if (($year_to > $year_from) || (($year_to == $year_from) && ($month_to > $month_from)) ||
            (($year_to == $year_from) && ($month_to == $month_from) && ($day_to >= $day_from)))
        return true;
    else return false;
}

function check_dates_now($date_f, $now) {
    $date_f = trim($date_f);
    $date_from_array = explode('/', $date_f);
    $date_now = explode('-', $now);
    $day_from = $date_from_array[0]; $month_from =  $date_from_array[1]; $year_from =  $date_from_array[2];
    $year_now = $date_now[0]; $month_now =  $date_now[1]; $day_now =  $date_now[2][0].$date_now[2][1];
    if (strlen($day_from)<2) $day_from='0'.$day_from; if (strlen($month_from)<2) $month_from='0'.$month_from;

    if (($year_from > $year_now) || (($year_from == $year_now) && ($month_from > $month_now)) ||
            (($year_from == $year_now) && ($month_from == $month_now) && ($day_from > $day_now)))
        return true;
    else return false;
}

function send_mail($to, $name, $subject, $body) {
    $mail             = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port

$mail->Username   = "office.sib011@gmail.com";  // GMAIL username
$mail->Password   = "sibsib011";            // GMAIL password

$mail->From       = "office.sib011@gmail.com";
$mail->FromName   = "StayInBelgrade";
$mail->Subject    = $subject;
//$mail->AltBody    = "This is the body when user views in plain text format"; //Text Body
$mail->WordWrap   = 50; // set word wrap

$mail->Body=$body;

$mail->AddAddress($to, $name);

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message has been sent";
}
}

$con=dbConnect("sib");
$hid = $_GET["hid"];
$uid = $_GET["uid"];
$hname = $_GET["hname"];

$name = $_POST["name"];
$surname = $_POST["surname"];
$mail = $_POST["mail"];
$addr = $_POST["addr"];
$phone = $_POST["phone"];
$beds = $_POST["beds"];
$from = $_POST["from"];
$to = $_POST["to"];

$timest = mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"), date("Y"));
$today = date("Y-m-d H:i:s", $timest);


if (($name == "") || ($surname == "") || ($mail == "") || ($addr == "") || ($phone == "") ||
        ($beds == "") || ($from == "") || ($to == ""))
    header('Location: ../hostels.php?err=7');
if (!check_beds($beds))
    header('Location: ../hostels.php?err=8');
if (!check_date($from))
    header('Location: ../hostels.php?err=9');
if (!check_date($to))
    header('Location: ../hostels.php?err=10');
if (check_date($from) && check_date($to) && !check_dates($from, $to))
    header('Location: ../hostels.php?err=11');
if(check_date($from) && !check_dates_now($from, $today))
    header('Location: ../hostels.php?err=12');

// mail koji automatski stize korisniku nakon sto rezervise hostel
$t = $mail; $n_s = $name.' '.$surname;
$subject = "Information about reservation";
$message = "<html><head></head><body>Dear $n_s <br /><br />
            You will be notified of the outcome of your reservations for the hostel $hname.
            for the period of 24 hours. <br />Thank you. <br /><br />Best regards,<br />
            StayInBelgrade</body></html>";
send_mail($t, $n_s, $subject, $message);





function change_date_format($date) {  
    $new = explode('/',$date);
    if (strlen($new[1])<2) $new[1]='0'.$new[1];
    if (strlen($new[0])<2) $new[0]='0'.$new[0];
    $d = $new[2].'-'.$new[1].'-'.$new[0];
    return $d;
}

if (($name != "") && ($surname != "") && ($mail != "") && ($addr != "") && ($phone != "") &&
        ($beds != "") && ($from != "") && ($to != "") && check_beds($beds) && check_date($from)
        && check_date($to) && check_dates($from, $to) && check_dates_now($from, $today)) {

$from_date = change_date_format($from);
$to_date = change_date_format($to);

$sql="INSERT INTO reservation (NumOfBeds, DateFrom, DateTo, ReservationDate, Status, IDHostel, IDUser)
            VALUES ('$beds', '$from_date', '$to_date', '$today', 'Pending', '$hid', '$uid')";

if (!mysql_query($sql, $con)) {
     die('Error: ' . mysql_error());
}
else 
     header('Location: ../hostel.php?hid='.$hid.'');

        }

mysql_close($con);


?>
